//
//  PainterUserView.m
//  p4
//
//  Created by CaoJun on 15/5/19.
//  Copyright (c) 2015年 com.baidao. All rights reserved.
//

#import "PainterUserView.h"

using namespace std;
using namespace ytx;

@interface PainterUserView()

@end

@implementation PainterUserView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _drawSubline = NO;
    }
    return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        _drawSubline = NO;
    }
    return self;
}

//不需要painter
- (void)drawRect:(CGRect)cgrect
{
    if (![self hasData]){
        return;
    }
    
    CGContextRef context = UIGraphicsGetCurrentContext();
    
    ChartCanvas * ctx = [ChartCanvas new];
    ctx.context = context;
    ctx.bounds = self.bounds;
    
    vector<UserGraph> userGraphs;
    if (self.indexPosition->getData()->getQuotationType() ==  LineType::LAVG) {
        AVGData * data = static_cast<AVGData *>(self.indexPosition->getData());
        userGraphs = data->getUserGraphs();
    }
    else if (LineType::isKLine(self.indexPosition->getData()->getQuotationType())) {
        KLineData * data = static_cast<KLineData *>(self.indexPosition->getData());
        userGraphs = data->getUserGraphs();
    }
    
    PainterUserDataBase * painter = (PainterUserDataBase *)self.painter;
    
    for (vector<UserGraph>::iterator it = userGraphs.begin(); it != userGraphs.end(); ++it) {
        UserGraph userGraph = (*it);
        BOOL isDrawSubline = NO;
        
        if ((it - userGraphs.begin()) == userGraphs.size() - 1) {
            isDrawSubline = self.isDrawSubline;
        }
        
        switch (userGraph.type) {
            case UserGraphType::UserGraphLine:
                [painter drawLine:self.bounds withContext:ctx withViewModelIndexPosition:self.indexPosition withUserGraph:userGraph withDrawSubline:isDrawSubline];
                break;
            case UserGraphType::UserGraphRectangle:
                [painter drawRectangle:self.bounds withContext:ctx withViewModelIndexPosition:self.indexPosition withUserGraph:userGraph withDrawSubline:isDrawSubline];
                break;
            default:
                break;
        }
    }

    [painter drawHoldingPositionInfosWithContext:ctx viewModelIndexPosition:self.indexPosition];
}


@end
